Skip to content

Sort character by frequency#7391

Open
lieutenant-Rohit wants to merge 5 commits intoTheAlgorithms:masterfrom
lieutenant-Rohit:SortCharacterByFrequency
Open

Sort character by frequency#7391
lieutenant-Rohit wants to merge 5 commits intoTheAlgorithms:masterfrom
lieutenant-Rohit:SortCharacterByFrequency

Conversation

@lieutenant-Rohit
Copy link
Copy Markdown

Added SortCharacterByFrequency algorithm using HashMap and Priority Queue

  • Counts frequency of characters using HashMap
  • Uses a max-heap (PriorityQueue) to sort characters by frequency
  • Includes JUnit test cases for validation
  • Code formatted using clang-format and follows project conventions

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.55%. Comparing base (b3e31b5) to head (732517c).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7391      +/-   ##
============================================
+ Coverage     79.53%   79.55%   +0.02%     
- Complexity     7176     7184       +8     
============================================
  Files           798      799       +1     
  Lines         23467    23486      +19     
  Branches       4617     4621       +4     
============================================
+ Hits          18665    18685      +20     
  Misses         4055     4055              
+ Partials        747      746       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@felseje felseje left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through the Checkstyle findings and left some comments to help guide the necessary changes.

int diff = b.getValue() - a.getValue();

// If frequency same, sort by character
if (diff == 0) return a.getKey() - b.getKey();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a single-line if statement like:

if (diff == 0) return a.getKey() - b.getKey();

please use braces:

if (diff == 0) {
return a.getKey() - b.getKey();
}

@@ -0,0 +1,66 @@
// Reference: https://leetcode.com/problems/sort-characters-by-frequency/
package com.thealgorithms.strings;
import java.util.*;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using wildcard imports (e.g., java.util.*). Please import only the specific classes you need.

@@ -0,0 +1,49 @@
package com.thealgorithms.strings;

import static org.junit.jupiter.api.Assertions.*;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using wildcard imports (e.g., org.junit.jupiter.api.Assertions.*). Please import only the specific classes you need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants